package com.ququ.syb.payorder.service;

import com.ququ.syb.common.constant.PayOrderStatusConstant;
import com.ququ.syb.common.constant.RedisKeyConstant;
import com.ququ.syb.common.pojo.PayOrder;
import com.ququ.syb.payorder.dao.PayOrderDao;
import com.ququ.syb.payorder.redis.RedisDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@EnableTransactionManagement
public class PayOrderService {
    @Autowired
    private PayOrderDao payOrderDao;
    @Autowired
    private PayOrderIdSequenceService payOrderIdSequenceService;
    @Autowired
    private RedisDao redisDao;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Logger logger = LoggerFactory.getLogger(PayOrderService.class);


    @Cacheable(value = "cache", key = "'payorder:'+#tradenum", unless = "#result == null")
    public PayOrder getPayOrder(String tradenum){
        return payOrderDao.getPayOrder(tradenum);
    }


    public PayOrder getPayOrder(String orderno, String appcode){
        return payOrderDao.getPayOrder(orderno, appcode);
    }

    public List<PayOrder> listPayOrder(Date beginDate, Date endDate, int currentpage, int pagesize, Integer ordertype, String paychannelid, String accountid, String appcode){
        return payOrderDao.listPayOrder(beginDate, endDate, currentpage, pagesize, ordertype, paychannelid, accountid, appcode);
    }

    @Transactional
    @CachePut(value = "cache", key = "'payorder:'+#result.tradenum", unless = "#result == null")
    public PayOrder save(PayOrder payOrder){
        logger.debug("保存订单前订单原始数据:{}",payOrder);
        String tradenum = payOrderIdSequenceService.getNextTradenum();
        Long id = payOrderIdSequenceService.getNextId();
        payOrder.setTradenum(tradenum);
        payOrder.setId(id);
        payOrder.setCreatetime(new Date());
        payOrder.setStatus(PayOrderStatusConstant.DEFAULT);
        payOrderDao.save(payOrder);
        payOrder = payOrderDao.getPayOrder(tradenum);
        Jedis jedis = redisDao.getJedis();
        jedis.setex(RedisKeyConstant.PAYORDER_TIMEOUT+tradenum, (int) ((payOrder.getTimeout().getTime()-payOrder.getCreatetime().getTime())/1000L),simpleDateFormat.format(payOrder.getTimeout()));
        jedis.sadd(RedisKeyConstant.PAYORDER_TIMEOUT_SET,tradenum);
        redisDao.close(jedis);
        return payOrder;
    }


    @Transactional
    @CachePut(value = "cache", key = "'payorder:'+#payOrder.tradenum", unless = "#result == null")
    public PayOrder update(PayOrder payOrder){
        payOrderDao.update(payOrder);
        return payOrderDao.getPayOrder(payOrder.getTradenum());
    }

}
